package com.tianyang.yukuaifu.business.auth.view;

import android.app.Activity;
import android.content.Context;
import android.net.Uri;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.tianyang.yukuaifu.R;
import com.tianyang.yukuaifu.business.auth.bean.CompanyLegalInput;
import com.tianyang.yukuaifu.business.auth.bean.IdCardOutput;
import com.tianyang.yukuaifu.business.auth.bean.SampleImageOutput;
import com.tianyang.yukuaifu.business.auth.contract.AuthConfig;
import com.tianyang.yukuaifu.common.cache.CacheManage;
import com.tianyang.yukuaifu.common.cache.CacheModel;
import com.tianyang.yukuaifu.common.interfaces.CommonConfig;
import com.tianyang.yukuaifu.common.utils.DateCheckPopListener;
import com.tianyang.yukuaifu.module.dialog.DateValidDialog;
import com.tianyang.yukuaifu.module.dialog.SampleImageDialog;
import com.tianyang.yukuaifu.module.dialog.interfaces.IDateValidListener;
import com.tianyang.yukuaifu.module.wheel.DateWheel;
import com.tianyang.yukuaifu.module.wheel.interfaces.IWheelDataChangeCallback;
import com.tianyang.yukuaifu.utils.DateUtils;
import com.tianyang.yukuaifu.utils.SoftInputUtil;
import com.tianyang.yukuaifu.utils.ToastUtil;
import com.tianyang.yukuaifu.utils.VerifyUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Description：企业认证法人信息
 * Created by xyj on 2024/3/10
 */
public class AuthCompanyLegalView extends LinearLayout {

    @BindView(R.id.idCardFrontImgSdv)
    SimpleDraweeView idCardFrontImgSdv;
    @BindView(R.id.idCardBackImgSdv)
    SimpleDraweeView idCardBackImgSdv;
    @BindView(R.id.handImgSdv)
    SimpleDraweeView handImgSdv;
    @BindView(R.id.xinmingEt)
    EditText xinmingEt;
    @BindView(R.id.shenfengidEt)
    EditText shenfengidEt;
    @BindView(R.id.shenfengbegindateTv)
    TextView shenfengbegindateTv;
    @BindView(R.id.shenfengenddateTv)
    TextView shenfengenddateTv;
    @BindView(R.id.lawyerAddressEt)
    EditText lawyerAddressEt;
    @BindView(R.id.nextStepLegalTv)
    Button nextStepLegalTv;
    @BindView(R.id.handImgLl)
    LinearLayout handImgLl;

    private SampleImageDialog vSampleImageDialog;

    private DateValidDialog vDateValidDialog;

    private DateCheckPopListener mDateStartPopListener;

    private DateCheckPopListener mDateEndPopListener;

    private CompanyLegalInput mCompanyLegalInput;

    private SampleImageOutput mOutput;

    private String posChannel;

    public AuthCompanyLegalView(Context context, AttributeSet attrs) {
        super(context, attrs);
        View.inflate(getContext(), R.layout.view_auth_company_legal, this);
        ButterKnife.bind(this);
        mCompanyLegalInput = new CompanyLegalInput();
        mOutput = CacheManage.getInstance().getCache(CacheModel.SAMPLE_IMAGE);
    }

    @OnClick({R.id.shenfengbegindateTv, R.id.shenfengenddateTv, R.id.takeIdCardFrontTv, R.id.takeIdCardBackTv, R.id.takeHandTv})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.shenfengbegindateTv:
                if (mDateStartPopListener == null) {
                    mDateStartPopListener = new DateCheckPopListener((Activity) getContext(), true);
                }
                mDateStartPopListener.handleRegionCheck(view, "请选择有效期开始日期", shenfengbegindateTv, new IWheelDataChangeCallback() {
                    @Override
                    public void wheelDataChangeCallback(String wheelData) {
                        if (!TextUtils.isEmpty(wheelData)) {
                            mCompanyLegalInput.setShenfengbegindate(wheelData);
                        }
                    }
                });
                break;
            case R.id.shenfengenddateTv:
                if (vDateValidDialog == null) {
                    vDateValidDialog = new DateValidDialog(getContext());
                }
                vDateValidDialog.setListener(new IDateValidListener() {
                    @Override
                    public void onDateValid(boolean isLongTerm) {
                        if (isLongTerm) {
                            shenfengenddateTv.setText("长期");
                            mCompanyLegalInput.setShenfengenddate(CommonConfig.DATE_LONG_TERM);
                        } else {
                            view.postDelayed(new Runnable() {
                                @Override
                                public void run() {
                                    showEndValidDialog(view);
                                }
                            }, 100);
                        }
                    }
                });
                vDateValidDialog.show();
                break;
            case R.id.takeIdCardFrontTv:
                if (mOutput != null) {
                    showSampleImageDialog(mOutput.getShenfengzhengpic());
                }
                break;
            case R.id.takeIdCardBackTv:
                if (mOutput != null) {
                    showSampleImageDialog(mOutput.getShenfengfanpic());
                }
                break;
            case R.id.takeHandTv:
                if (mOutput != null) {
                    showSampleImageDialog(mOutput.getHoldingCardPic());
                }
                break;
        }
    }

    private void showSampleImageDialog(SampleImageOutput.SampleImage sampleImage) {
        if (vSampleImageDialog == null) {
            vSampleImageDialog = new SampleImageDialog(getContext());
        }
        vSampleImageDialog.showPictureDataView(sampleImage);
        vSampleImageDialog.show();
    }

    private void showEndValidDialog(View view) {
        if (mDateEndPopListener == null) {
            mDateEndPopListener = new DateCheckPopListener((Activity) getContext(), true);
        }
        mDateEndPopListener.handleRegionCheck(view, "请选择有效期结束日期", shenfengenddateTv, DateWheel.DATE_TYPE_ALL,
                new IWheelDataChangeCallback() {
                    @Override
                    public void wheelDataChangeCallback(final String wheelData) {
                        if (!TextUtils.isEmpty(wheelData)) {
                            mCompanyLegalInput.setShenfengbegindate(wheelData);
                        }
                    }
                });
    }

    public void onFeedBackView(CompanyLegalInput input, String posChannel) {
        this.posChannel = posChannel;
        if (input != null) {
            mCompanyLegalInput = input;
            if (!TextUtils.isEmpty(mCompanyLegalInput.getShenfengzhengpic()))
                idCardFrontImgSdv.setImageURI(Uri.parse(mCompanyLegalInput.getShenfengzhengpic()));
            if (!TextUtils.isEmpty(mCompanyLegalInput.getShenfengfanpic()))
                idCardBackImgSdv.setImageURI(Uri.parse(mCompanyLegalInput.getShenfengfanpic()));
            if (!TextUtils.isEmpty(mCompanyLegalInput.getHoldingCardPic()))
                handImgSdv.setImageURI(Uri.parse(mCompanyLegalInput.getHoldingCardPic()));
            xinmingEt.setText(VerifyUtil.checkString(mCompanyLegalInput.getXinming()));
            shenfengidEt.setText(VerifyUtil.checkString(mCompanyLegalInput.getShenfengid()));
            shenfengbegindateTv.setText(VerifyUtil.checkString(mCompanyLegalInput.getShenfengbegindate()));
            if (TextUtils.equals(CommonConfig.DATE_LONG_TERM, mCompanyLegalInput.getShenfengenddate())) {
                shenfengenddateTv.setText("长期");
            } else {
                shenfengenddateTv.setText(VerifyUtil.checkString(mCompanyLegalInput.getShenfengenddate()));
            }
            lawyerAddressEt.setText(VerifyUtil.checkString(mCompanyLegalInput.getLawyerAddress()));
            if (TextUtils.equals(AuthConfig.POS_CHANNEL_SXY, mCompanyLegalInput.getPosChannel())) {
                handImgLl.setVisibility(VISIBLE);
            } else {
                handImgLl.setVisibility(GONE);
            }
        }
        if (TextUtils.equals(AuthConfig.POS_CHANNEL_FY, posChannel)) {
            handImgLl.setVisibility(View.VISIBLE);
        } else {
            handImgLl.setVisibility(View.GONE);
        }
    }

    public void showIdCardInfoView(IdCardOutput output) {
        if (output != null) {
            if (TextUtils.equals("1", output.getCodeType())) {
                xinmingEt.setText(VerifyUtil.checkString(output.getName()));
                mCompanyLegalInput.setXinming(output.getName());
                shenfengidEt.setText(VerifyUtil.checkString(output.getCode()));
                mCompanyLegalInput.setShenfengid(output.getCode());
                lawyerAddressEt.setText(VerifyUtil.checkString(output.getAddress()));
                mCompanyLegalInput.setLawyerAddress(output.getAddress());
            } else if (TextUtils.equals("2", output.getCodeType())) {
                if (!TextUtils.isEmpty(output.getIssueDate())) {
                    String issueDate = DateUtils.formatTime(output.getIssueDate(), "yyyyMMdd", "yyyy-MM-dd");
                    shenfengbegindateTv.setText(issueDate);
                    mCompanyLegalInput.setShenfengbegindate(issueDate);
                }
                if (!TextUtils.isEmpty(output.getExpiryDate())) {
                    String expiryDate = DateUtils.formatTime(output.getExpiryDate(), "yyyyMMdd", "yyyy-MM-dd");
                    shenfengenddateTv.setText(expiryDate);
                    mCompanyLegalInput.setShenfengenddate(expiryDate);
                }
            }
        }
    }

    public void showPhotoView(String urlStr, String photoType) {
        switch (photoType) {
            case CommonConfig.TYPE_IDFROPIC:
                idCardFrontImgSdv.setImageURI(Uri.parse(urlStr));
                mCompanyLegalInput.setShenfengzhengpic(urlStr);
                break;
            case CommonConfig.TYPE_IDCONPIC:
                idCardBackImgSdv.setImageURI(Uri.parse(urlStr));
                mCompanyLegalInput.setShenfengfanpic(urlStr);
                break;
            case CommonConfig.TYPE_IDHANDPIC:
                handImgSdv.setImageURI(Uri.parse(urlStr));
                mCompanyLegalInput.setHoldingCardPic(urlStr);
                break;
        }
    }

    public CompanyLegalInput getCompanyLegalInput() {
        mCompanyLegalInput.setXinming(xinmingEt.getText().toString());
        mCompanyLegalInput.setShenfengid(shenfengidEt.getText().toString());
        mCompanyLegalInput.setLawyerAddress(lawyerAddressEt.getText().toString());
        return mCompanyLegalInput;
    }

    public boolean checkCompanyLegal() {
        if (TextUtils.isEmpty(mCompanyLegalInput.getShenfengzhengpic())) {
            ToastUtil.show("请上传身份证人像面");
            return false;
        }
        if (TextUtils.isEmpty(mCompanyLegalInput.getShenfengfanpic())) {
            ToastUtil.show("请上传身份证国徽面");
            return false;
        }
        if ((TextUtils.equals(AuthConfig.POS_CHANNEL_SXY, mCompanyLegalInput.getPosChannel())
                || TextUtils.equals(AuthConfig.POS_CHANNEL_FY, posChannel))
                && TextUtils.isEmpty(mCompanyLegalInput.getHoldingCardPic())) {
            ToastUtil.show("请上传手持身份证照");
            return false;
        }
        if (TextUtils.isEmpty(xinmingEt.getText())) {
            ToastUtil.show("请输入证件姓名");
            SoftInputUtil.openKeybord(xinmingEt, getContext());
            return false;
        }
        if (TextUtils.isEmpty(shenfengidEt.getText())) {
            ToastUtil.show("请输入证件号码");
            SoftInputUtil.openKeybord(shenfengidEt, getContext());
            return false;
        }
        if (TextUtils.isEmpty(mCompanyLegalInput.getShenfengbegindate())) {
            ToastUtil.show("请选择证件有效期始");
            return false;
        }
        if (TextUtils.isEmpty(mCompanyLegalInput.getShenfengenddate())) {
            ToastUtil.show("请选择证件有效期至");
            return false;
        }
        if (TextUtils.isEmpty(lawyerAddressEt.getText())) {
            ToastUtil.show("请填写身份证地址");
            SoftInputUtil.openKeybord(lawyerAddressEt, getContext());
            return false;
        }
        return true;
    }

    public void setOnlyReadView() {
        xinmingEt.setEnabled(false);
        shenfengidEt.setEnabled(false);
        shenfengbegindateTv.setEnabled(false);
        shenfengbegindateTv.setCompoundDrawables(null, null, null, null);
        shenfengenddateTv.setEnabled(false);
        shenfengenddateTv.setCompoundDrawables(null, null, null, null);
        lawyerAddressEt.setEnabled(false);
        nextStepLegalTv.setVisibility(GONE);
    }

    public void onDestroyView() {
        if (vSampleImageDialog != null && vSampleImageDialog.isShowing()) {
            vSampleImageDialog.dismiss();
            vSampleImageDialog = null;
        }
        if (vDateValidDialog != null) {
            vDateValidDialog.dismiss();
            vDateValidDialog = null;
        }
    }
}
